#!/usr/bin/python3
# _*_ coding: utf-8 _*_
#
# Copyright (C) 2024 - 2024 heihieyouheihei, Inc. All Rights Reserved 
#
# @Time    : 2024/9/21 22:17
# @Author  : Yuyun
# @File    : 前序和中序构造二叉树and计算二叉树高度.py
# @IDE     : PyCharm

"""前序求的就是深度，使用后序求的是高度。"""
"""根节点的高度就是二叉树的最大深度"""

class TreeNode:
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
from collections import deque
class Solution:
    def build(self, preorder, midorder)->TreeNode:
        if not preorder:
            return None
        root = TreeNode(preorder[0])
        root_index = midorder.index(root.val)
        mid_left = midorder[:root_index]
        mid_right = midorder[root_index + 1:]
        pre_left = preorder[1:len(mid_left) + 1]
        pre_right = preorder[len(mid_left) + 1:]
        root.left = self.build(pre_left, mid_left)
        root.right = self.build(pre_right, mid_right)
        return root
      # 前序遍历求最大深度 = 高度
    def preorder_traversal_height(self, root, depth = 1):
        #   一开始默认根节点深度为1
        if root.left == None and root.right == None:
            return depth
        #   中间节点深度
        root_depth = depth
        left_max_depth = right_max_depth = depth
        #   左节点最大深度
        if root.left:
            left_max_depth = self.preorder_traversal_height(root.left, root_depth + 1)
        #   右节点最大深度
        if root.right:
            right_max_depth = self.preorder_traversal_height(root.right, root_depth + 1)
        #   返回最大深度 = 高度
        return max(left_max_depth, right_max_depth)

    #   后序遍历求高度
    def postorder_traversal_height(self,root, height = 0):
        #   空节点高度为0
        if not root:
            return 0
        #   左
        left_height = self.postorder_traversal_height(root.left, height)
        #   右
        right_height = self.postorder_traversal_height(root.right, height)
        #   中 = max(左子树高度，右子树高度) + 1
        height = 1 + max(left_height, right_height)
        return height

    #   层序遍历求高度
    def levelorder_traversal_height(self, root):
        if not root:
            return 0
        depth = 0
        queue_node = deque()
        queue_node.append(root)
        while queue_node:
            #   遍历一层
            depth += 1
            for _ in range(len(queue_node)):
                node = queue_node.popleft()
                if node.left:
                    queue_node.append(node.left)
                if node.right:
                    queue_node.append(node.right)
        #   最后一层深度 = 树的高度
        return depth

if __name__ =="__main__":
    solution = Solution()
    while True:
        try:
            n = int(input())
            preorder = list(input().strip())
            midorder = list(input().strip())
            root = solution.build(preorder, midorder)
            print(solution.preorder_traversal_height(root))
        except:
            break